#include "login.h"
#include "ui_login.h"

extern bool WinShowFlag;
extern QString USER;
extern QString USERTYPE;
Login::Login(datebase *db,QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Login)
{
    ui->setupUi(this);
    setWindowFlags(Qt::FramelessWindowHint);
    login_mydb=db;
    ui->lineEdit_Pass->setEchoMode(QLineEdit::Password);
    ui->lineEdit_Pass->setStyleSheet("lineedit-password-character:42");
    GetUser();
   //setAttribute(Qt::WA_TranslucentBackground, true);
}

Login::~Login()
{
    delete ui;
}

void Login::on_pushButton_LoginOk_clicked()
{
    if(ui->lineEdit_Pass->text().isEmpty())
    {
        QMessageBox::information(this,QObject::trUtf8("提示"), QObject::trUtf8("密码为空"));
        return;
    }
    QString data,password;
    QStringList list;
    data=this->login_mydb->selectUserData(&this->login_mydb->sqlite_db,ui->comboBox_Name->currentText());
    list=data.split("-");
    password=list[0];
    if(ui->lineEdit_Pass->text()== password)
    {
        WinShowFlag=true;
        USER=ui->comboBox_Name->currentText();
        USERTYPE=list[1];
        this->close();
    }else{
        QMessageBox::information(this,QObject::trUtf8("提示"), QObject::trUtf8("密码错误"));
        return;
    }
}

void Login::on_pushButton_Quit_clicked()
{
    WinShowFlag=false;
    this->close();
}

void Login::GetUser()
{
    QSqlQuery select_name(login_mydb->sqlite_db);
    QString cmd = "select userName from user";
    select_name.exec(cmd);
    if(select_name.isActive())
    {
        QStringList strList;
        while(select_name.next())
        {
            strList<<select_name.value(0).toString();
        }
        ui->comboBox_Name->addItems(strList);
    }
    select_name.clear();
}

